<template>
  <div style="height: 6vh;background-color:#fff;padding: 10px 20px;">
    <span style="line-height:60px;font-size:20px;">用户管理</span>
  </div>
  <!-- 新建用户弹窗 -->
  <!-- :before-close="handleClose" 在弹窗关闭前先调用handleClose方法来处理弹窗的关闭问题
   避免点击弹窗外部也能关闭弹窗-->

  <!-- 用户搜索卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="用户搜索" style="width: 220px;">
        <el-input placeholder="输入用户名" v-model="searchUserForm.username" @keydown.enter="loadUser"></el-input>
      </el-form-item>
      <el-form-item label="用户状态" style="width: 220px;">
        <el-input placeholder="请输入用户状态" v-model="searchUserForm.status" @change="loadUser">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadUser">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 用户表格卡片 -->
  <el-card style="margin: 20px;">
    <el-table :data="userArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" align="center"></el-table-column>
      <el-table-column prop="email" label="用户邮箱" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
      <el-table-column prop="registrationDate" label="用户注册时间" align="center"></el-table-column>
      <el-table-column prop="status" label="用户状态" align="center"></el-table-column>
      <el-table-column prop="memberLevel" label="会员等级" align="center"></el-table-column>
    </el-table>
    <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    ></el-pagination>
  </el-card>
</template>

<script setup>
import {computed, onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";


const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0)
const paginatedData = computed(() => {
  if (!Array.isArray(userArr.value)) return [];
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  return userArr.value.slice(start, end);
});
const handleSizeChange = (newSize) => {
  pageSize.value = newSize;
  currentPage.value = 1; // 重置当前页为第一页
  loadUser();
};

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage;
  loadUser();
};



//定义对象用来保存用户的查询条件
const searchUserForm = ref({username:'', status:''});
//定义对象保存查询到的用户表格数据
const userArr = ref([]);
//定义加载用户的方法
const loadUser = ()=>{
  let data =  qs.stringify({
    ...searchUserForm.value,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  });
  //如果有查询条件,就根据后面拼接的条件查,没有条件默认加载所有用户数据
  axios.get(BASE_URL+'/v1/user/select?'+data)
      .then((response)=>{//回调函数中处理响应结果
        if(response.data.code == 2000){
          const pageInfo = response.data.data;
          userArr.value = pageInfo.list || []; // 将后端响应数据装入员工数组中
          total.value = pageInfo.total; // 更新总条数
          console.log(userArr.value);
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
//定义重置搜索条件的方法
const resetSearch = ()=>{
  //清空之前的搜索条件
  searchUserForm.value = {};
  //重新加载所有的用户数据
  loadUser();
}

/* 页面一加载完毕,立即执行 */
onMounted(()=>{
  loadUser();
})



//定义编辑用户的方法


//定义修改员工状态的方法
const changeStatus = (userId,status)=>{
  axios.post(BASE_URL+'/v1/user/update/status/'+userId+'/'+status)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success('修改状态成功!');
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}


</script>

<style scoped>

</style>